package org.huzhp.audit.controller;

import com.github.pagehelper.Page;
import org.huzhp.audit.entity.RequestLog;
import org.huzhp.audit.entity.UserLog;
import org.huzhp.audit.entity.vo.AuditLogVo;
import org.huzhp.audit.service.IAuditLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description: 接口
 * @Author: huzp
 * @CreateDate: 2020/4/10 10:26
 * @Version: 1.0
 */

@RestController
@RequestMapping("audit")
public class AuditController {

    @Autowired
    private IAuditLogService auditLogService;


    @GetMapping("logs")
    public Page logs(AuditLogVo auditLogVo, Integer pageNum, Integer pageSize) {
        pageSize = (pageSize == null) ? 10 : pageSize;
        pageNum = (pageNum == null) ? 1 : pageNum;
        return auditLogService.pageAuditLog(auditLogVo, pageNum, pageSize);
    }


    @PostMapping("userLog")
    public boolean userLog(UserLog userLog) {
        try {
            userLog.setUserTime(new Date());
            auditLogService.saveUserLog(userLog);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @PostMapping("requestLog")
    public boolean requestLog(RequestLog requestLog) {
        try {
            auditLogService.saveRequestLog(requestLog);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
